// API配置文件
// 使用相对路径但包含端口，这样服务器IP改变时无需修改程序
export const API_BASE_URL = "http://"+window.location.hostname+':7777'+'/api/v1'; // 基础路径，端口在axios实例中配置

// WebSocket配置
export const WS_BASE_URL = "ws://"+window.location.hostname+':7777'+'/api/v1'; // WebSocket基础路径

// 各模块API地址
export const API_URLS = {
  // 用户相关
  user: {
    accessToken: `${API_BASE_URL}/user/access_token`,
    baseToken: `${API_BASE_URL}/base/access_token`
  },

  // AGV相关
  agv: {
    base: `${API_BASE_URL}/agv`,
    position: `${API_BASE_URL}/agv/position`,
    createsimulation: `${API_BASE_URL}/agv/create_simulation`,
    deletesimulation: `${API_BASE_URL}/agv/delete_simulation`,
    enable: `${API_BASE_URL}/agv/enable`,
    disable: `${API_BASE_URL}/agv/disable`,
    page: `${API_BASE_URL}/agv/page`,
    detail: `${API_BASE_URL}/agv/detail`,
    update: `${API_BASE_URL}/agv/update`,
    create: `${API_BASE_URL}/agv/create`,
    delete: `${API_BASE_URL}/agv/delete`,
    nameList: `${API_BASE_URL}/agv/namelist`,
    update_station_to: `${API_BASE_URL}/agv/update_station_to`,
  },

  // 站点相关
  map_node: {
    all: `${API_BASE_URL}/map/all`,
    node: `${API_BASE_URL}/map/all/node`,
    page: `${API_BASE_URL}/map/page`,
    delete: `${API_BASE_URL}/map/delete`,
    detail: `${API_BASE_URL}/map/detail`,
    create: `${API_BASE_URL}/map/create`,
    update: `${API_BASE_URL}/map/update`,
    nameList: `${API_BASE_URL}/map/namelist`,
    nodeNameList: `${API_BASE_URL}/map/node/namelist`,
  },

  // 地图相关
  map_edge: {
    lineList: `${API_BASE_URL}/map/edge/line_list`,
    list: `${API_BASE_URL}/map/edge/list`,
    create: `${API_BASE_URL}/map/edge/create`,
    delete: `${API_BASE_URL}/map/edge/delete`,
  },

  // 任务相关
  tasks: {
    allStatus: `${API_BASE_URL}/tasks/allstatus`,
    start: `${API_BASE_URL}/tasks/start`,
    stop: `${API_BASE_URL}/tasks/stop`,
    restart: `${API_BASE_URL}/tasks/restart`
  },

  // 分拣模拟相关
  luosi: {
    base: `${API_BASE_URL}/luosi`,
    state: `${API_BASE_URL}/luosi/state`,
    stepRun: `${API_BASE_URL}/luosi/step-run`,
    reset: `${API_BASE_URL}/luosi/reset`,
    toggleAutoRun: `${API_BASE_URL}/luosi/toggle-auto-run`,
    changeStationSize: `${API_BASE_URL}/luosi/change-station-size`,
    changeProductTypesCount: `${API_BASE_URL}/luosi/change-product-types-count`,
    toggleProductGenerationMode: `${API_BASE_URL}/luosi/toggle-product-generation-mode`,
    forceReSort: `${API_BASE_URL}/luosi/force-re-sort`,
    init: `${API_BASE_URL}/luosi/init`,
    start: `${API_BASE_URL}/luosi/start`,
    stop: `${API_BASE_URL}/luosi/stop`,
    getStatus: `${API_BASE_URL}/luosi/getstatus`,
    websocket: `${WS_BASE_URL}/luosi/ws`, // WebSocket连接地址
  },

  // 订单相关
  order: {
    page: `${API_BASE_URL}/orders/page`,
    create: `${API_BASE_URL}/orders/create`,
    // update: `${API_BASE_URL}/orders/orders`,
    delete: `${API_BASE_URL}/orders/delete`,
  },
};

// 创建axios实例并配置基础URL
import axios from 'axios';

export const apiClient = axios.create({
  baseURL: "http://"+window.location.hostname+':7777'+'/api', // 基础URL，这样所有请求都会自动添加这个前缀
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 添加请求拦截器，在每个请求中添加token
apiClient.interceptors.request.use(
  config => {
    // 从cookie中获取token
    const token = getCookie('access_token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 辅助函数：从cookie获取值
function getCookie(name) {
  const nameEQ = `${name}=`;
  const cookies = document.cookie.split(';');
  for (let i = 0; i < cookies.length; i++) {
    let cookie = cookies[i];
    while (cookie.charAt(0) === ' ') cookie = cookie.substring(1);
    if (cookie.indexOf(nameEQ) === 0) {
      return decodeURIComponent(cookie.substring(nameEQ.length));
    }
  }
  return null;
}
